<?php
include("configurations/config.inc.php");	
	class db {
		var $host = dbhost;
		var $username = dbuser;
		var $password = dbpass;
		var $db_name = dbname;
		var $data;
		var $link;
		
		function connect(){
			$this->link = mysql_connect($this->host, $this->username, $this->password);
			if($this->link){ mysql_select_db($this->db_name); }
			return $this->link;
		}// end connect
		function insert($table_name,$field_names,$values){
			$sql = "INSERT INTO {$table_name} ({$field_names}) VALUES ({$values})";
			mysql_query("SET NAMES utf8");
			$qr = mysql_query($sql);
			if($qr){
				return $qr;
			}else{
			return $sql;
			}
			
		}//end insert
		function update($table_name,$field_to_set,$value_to_set,$condition=""){
			$field_arr = split(",", $field_to_set);
			$value_arr = split(",", $value_to_set);
			$update_group = "";
			for($i=0; $i<count($field_arr); $i++){
				if($i > 0){ $update_group .= ", "; }
				$update_group .= "{$field_arr[$i]} =  {$value_arr[$i]}";
			}
			$sql = "UPDATE {$table_name} SET {$update_group} {$condition}";
			mysql_query("SET NAMES utf8");
			$qr = mysql_query($sql);
			if($qr){
				return $qr; 
			}else{
				return $sql;
			}
		}// end update
		function delete ($table_name,$condition = ""){
			return mysql_query("DELETE FROM {$table_name} {$condition}");
		}// end delete
		function query($sql){ 
			mysql_query("SET NAMES UTF8");  
			$qr=mysql_query($sql);
			while ($row = mysql_fetch_array($qr)){       
			   $results[] = $row;
			}
			
			return $results;
		}// end select
		function num($sql){
			$qr = mysql_query($sql);
			return mysql_num_rows($qr);
		}
		function Paginate($values,$per_page){
			$total_values = count($values);
			if(isset($_GET['page'])){
				$current_page = $_GET['page'];
			}else{
				$current_page = 1;
			}

			$counts = ceil($total_values / $per_page);
			$paraml = ($current_page - 1)* $per_page;
			$this->data = array_slice($values,$paraml,$per_page);

			for($i=1; $i<=$counts; $i++){
				$numbers[] = $i;
			} return $numbers;
		}
		function fetchResult(){
			$resultsValues = $this->data;
			return $resultsValues;
		}
		function find_where($table_name, $field_name, $condition = ""){
   			return mysql_query("SELECT {$field_name} FROM {$table_name} {$condition}");
  		}//enc find_where
	}// end db
?>